/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import JDBCUtil.MyConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.admin.Advertisement;

/**
 *
 * @author Danh
 */
public class AdvertisementManager {

    Connection conn = null;

    public AdvertisementManager() {
        try {
            conn = MyConn.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Advertisement> getAdvertisement() {
        List<Advertisement> list = new ArrayList<Advertisement>();
        try {
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM Advertisements");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Advertisement ad = new Advertisement(rs.getInt("Advertisement_Id"), rs.getInt("Image_Id"), rs.getString("Expired_Date"), rs.getDate("Created_Date"), rs.getNString("Company_Issued"), rs.getBoolean("Advertisement_Status"));
                list.add(ad);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean insertAd(Advertisement ad) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO Advertisements (Image_Id,Expired_Date,Created_Date,Company_Issued,Advertisement_Status) VALUES (?,?,?,?,?)");
            pstmt.setInt(1, ad.getImageId());
            pstmt.setString(2, ad.getExprired_Date());
            pstmt.setDate(3, ad.getCreate_Date());
            pstmt.setNString(4, ad.getCompany_Issued());
            pstmt.setBoolean(5, ad.isAdverStatus());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
        }
        return false;
    }

    public boolean updateAd(Advertisement ad) {
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement("UPDATE Advertisements SET Image_Id = ?,Expired_Date = ?,Created_Date = ?,Company_Issued = ?,Advertisement_Status=? WHERE Advertisement_Id = ?");
            pstmt.setInt(1, ad.getImageId());
            pstmt.setString(2, ad.getExprired_Date());
            pstmt.setDate(3, ad.getCreate_Date());
            pstmt.setNString(4, ad.getCompany_Issued());
            pstmt.setBoolean(5, ad.isAdverStatus());
            pstmt.setInt(6, ad.getAdverId());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean delete(int id) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("DELETE FROM Advertisements  WHERE Advertisement_Id = ?");
            pstmt.setInt(1, id);
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
        }

        return false;
    }

    public static void main(String[] args) {
        AdvertisementManager mana = new AdvertisementManager();
        System.out.println(mana.getAdvertisement().size());
    }
}
